import cdsapi
import os
import calendar
import configparser
import datetime
import pandas as pd
import json
c = cdsapi.Client()


def download_file(year, month, day, out_path_file):
    c.retrieve(
        'reanalysis-era5-land',
        {
            'format': 'grib',
            'variable': [
                '2m_temperature', 'total_evaporation', 'volumetric_soil_water_layer_1',
                'volumetric_soil_water_layer_2', 'volumetric_soil_water_layer_3',
                'volumetric_soil_water_layer_4',
            ],
            'year': str(year),
            'month': month,
            'day': day,
            'time': [
                '00:00', '01:00', '02:00',
                '03:00', '04:00', '05:00',
                '06:00', '07:00', '08:00',
                '09:00', '10:00', '11:00',
                '12:00', '13:00', '14:00',
                '15:00', '16:00', '17:00',
                '18:00', '19:00', '20:00',
                '21:00', '22:00', '23:00',
            ],
        },
        out_path_file)


def download_era5(out_path, year, month, day):
    out_path_day = out_path + '/{0}/{1}'.format(str(year), month)
    if not os.path.exists(out_path_day):
        os.makedirs(out_path_day)
    out_path_file = out_path_day + '/{0}_{1}_{2}.grib'.format(str(year), month, day)
    if not os.path.isfile(out_path_file):
        print('{} file does not exist, download started.'.format(out_path_file))
        download_file(year, month, day, out_path_file)
    elif os.path.getsize(out_path_file) < 807140160:
        print("{} file is incomplete, download again.".format(out_path_file))
        download_file(year, month, day, out_path_file)
    else:
        print("{} file exists.".format(out_path_file))


if __name__ == "__main__":
    conf = configparser.ConfigParser()
    conf.read(os.getcwd() + '/config/config.ini', encoding="utf-8")
    start_day = conf.get('ERA5', 'start_day')
    end_day = conf.get('ERA5', 'end_day')
    out_path = conf.get('ERA5', 'out_path')
    current_time = datetime.datetime.now().strftime('%Y%m%d')
    times_range = pd.date_range(start_day, current_time, freq='1D')
    progress_dir = os.path.join(os.getcwd(), 'storage/progress')
    for time in times_range:
        str_time = time.strftime('%Y%m%d')
        if not os.path.exists(progress_dir):
            os.makedirs(progress_dir)
        progress_name = progress_dir + '/progress_real_time_{}.json'.format('ERA5')
        if not os.path.exists(progress_name):
            with open(progress_name, 'w') as of:
                json.dump(str_time, of)
        year_str = str_time[0:4]
        month_str = str_time[4:6]
        day_str = str_time[6:8]
        with open(progress_name, 'r') as of:
            progress_time = json.load(of)
        if int(str_time) >= int(progress_time):
            print("Current download time is: {}".format(str_time))
            try:
                download_era5(out_path, year_str, month_str, day_str)
                with open(progress_name, 'w') as of:
                    json.dump(str_time, of)
            except:
                print("Data download exception, please check!")
                break
